package models


import (
	"fmt"
	"github.com/go-xorm/xorm"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"WDserver/config"
)



var ADB,WG,LDB *xorm.Engine


func Init (){
	var err error
	dbIP := config.Config().Sqlip
	dbPort := config.Config().Sqlport
	dbUser := config.Config().Sqluser
	dbPwd := config.Config().Sqlpwd
	dbData1 := config.Config().Sqldata1
	dbData2 := config.Config().Sqldata2
	dbData3 := config.Config().Sqldata3
	dns1 := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&allowOldPasswords=1",dbUser,dbPwd,dbIP,dbPort,dbData1)
	dns2 := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&allowOldPasswords=1",dbUser,dbPwd,dbIP,dbPort,dbData2)
	dns3 := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&allowOldPasswords=1",dbUser,dbPwd,dbIP,dbPort,dbData3)
	ADB, err = xorm.NewEngine("mysql", dns1)
	if err != nil {
		log.Fatal("dl_adb_all数据库连接失败:", err)
	}
	if  err := ADB.Sync2(new(Account)); err != nil {
		log.Fatal("dl_adb_all.account数据表同步失败:", err)
	}
	WG, err = xorm.NewEngine("mysql", dns2)
	if err != nil {
		log.Fatal("dl_wg数据库连接失败:", err)
	}
	if  err := WG.Sync2(new(User)); err != nil {
		log.Fatal("dl_wg.user数据表同步失败:", err)
	}
	if  err := WG.Sync2(new(Cdk)); err != nil {
		log.Fatal("dl_wg.cdk数据表同步失败:", err)
	}

	LDB, err = xorm.NewEngine("mysql", dns3)
	if err != nil {
		log.Fatal("dl_ldb_1数据库连接失败:", err)
	}
	if  err := LDB.Sync2(new(LoginLog)); err != nil {
		log.Fatal("dl_ldb_1.login_log数据表同步失败:", err)
	}
}
